package com.darsystems.foghourz.server;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.darsystems.foghourz.client.UserWorkInfo;

public class ResponseParser {
	
	static public double parsePercent(Document responseDoc)
	{
		return parseValue(responseDoc, "nPercentTimeAllOtherProjects");
	}
	
	static public UserWorkInfo parseUserWorkInfo(Document responseDoc)
	{
		UserWorkInfo info = new UserWorkInfo();
		info.setPercentFBTime(parsePercent(responseDoc));

		double startHour = parseValue(responseDoc, "nWorkdayStarts");
		double endHour = parseValue(responseDoc, "nWorkdayEnds");
		if(endHour < startHour)
		{
			endHour += 24; 
		}
		
		double lunch = parseValue(responseDoc, "hrsLunchLength");
		
		info.setNumberWorkHours(endHour - startHour - lunch);	
		return info;
	}
	
	private static double parseValue(Document responseDoc, String tagName)
	{
		NodeList list = responseDoc.getElementsByTagName(tagName);
		if(list.getLength() == 1)
		{
			Element el = (Element)list.item(0);
			return Double.parseDouble(el.getTextContent());
		}
		
		return 0;
	}
	
}
